package com.cgs.query.server.controller

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import io.swagger.annotations.ApiParam
import com.cgs.query.domain.QueryDefine
import com.cgs.query.server.service.ServerQueryService
import com.cgs.query.service.IQueryDefineService
import com.cgs.query.service.IQueryService
import com.cgs.query.service.jpa.QueryDefineRepository
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/define")
@Api(description = "Query定义管理模块")
class QueryDefineController {

    @Autowired
    private lateinit var queryDefineService: IQueryDefineService

    @PostMapping("/list")
    @ApiOperation(value = "根据标签、定义名称查询")
    fun findByTagIdsAndDefineNameLike(@ApiParam(value = "当前页数") @RequestParam(required = false, defaultValue = "0") pn: Int,
                                      @ApiParam(value = "每页大小") @RequestParam(required = false, defaultValue = "5") ps: Int,
                                      @ApiParam(value = "查询名称") @RequestParam defineName: String,
                                      @ApiParam(value = "标签id集合") @RequestBody(required = false) tagIds: List<String>) = queryDefineService.findByTagNamesAndNameLike(pn, ps, tagIds, defineName)

    @PostMapping("/")
    @ApiOperation(value = "修改定义")
    fun save(@ApiParam(value = "查询定义", required = true) @RequestBody define: QueryDefine) = queryDefineService.save(define)

    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询定义")
    fun findOne(@ApiParam(value = "编号", required = true) @PathVariable id: String) = queryDefineService.findById(id)

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    fun delete(@ApiParam(value = "编号", required = true) @PathVariable id: String) = queryDefineService.delete(id)

    //----------------------------------------------

    @GetMapping("/list/{projectName}")
    @ApiOperation(value = "通过项目名称查询定义")
    fun findByProjectName(@ApiParam(value = "项目名称", required = false) @PathVariable(required = false) projectName: String) =
            queryDefineService.findByProjectName(projectName)

}

